package com.ysj.demo.accompanynetwork.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.adapter.PublishTabFragmentPagerAdapter;
import com.ysj.demo.accompanynetwork.fragment.PublishFindPlayerFragment;
import com.ysj.demo.accompanynetwork.fragment.PublishRentYourselfFragment;

import java.util.ArrayList;
import java.util.List;

public class PublishActivity extends BaseActivity implements View.OnClickListener {
    private TextView titleTv;
    private ImageView leftIv;
    private TabLayout tabL;
    private PublishFindPlayerFragment playerFl;
    private PublishRentYourselfFragment rentFl;
    private ViewPager pager;
    private PublishTabFragmentPagerAdapter adapter;
    private List<Fragment> list;
    private FragmentManager manager;

    private int type;// 1 找玩伴 2 出租自己

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        StatusBarUtil.fullScreen(this);

        initView();
        initListener();
        initData();
    }

    private void initView() {
        titleTv = findViewById(R.id.top_tv);
        titleTv.setText("发布");
        leftIv = findViewById(R.id.top_left_iv);
        leftIv.setVisibility(View.VISIBLE);
        leftIv.setOnClickListener(this);
        pager = findViewById(R.id.view_pager);
        tabL = findViewById(R.id.tab_layout);

        playerFl = new PublishFindPlayerFragment();
        rentFl = new PublishRentYourselfFragment();
        list = new ArrayList<>();
        list.add(playerFl);
        list.add(rentFl);
        manager = getSupportFragmentManager();
//        adapter = new PublishTabFragmentPagerAdapter(manager, list);
//        pager.setAdapter(adapter);
        ArrayList<String> mList = new ArrayList<>();
        mList.add("找个玩伴");
        mList.add("出租自己");


        pager.setAdapter(new MyAdapter(getSupportFragmentManager(), mList));
        /*Tab与ViewPager绑定*/
        tabL.setupWithViewPager(pager);
    }

    private void initListener() {

    }

    private void initData() {
        type = getIntent().getIntExtra("TYPE", 1);
        pager.setCurrentItem(type);
        tabL.setScrollPosition(type, 0, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_left_iv:
                finish();
                break;
        }
    }

    public class MyAdapter extends FragmentPagerAdapter {

        private List<String> mlist;

        public MyAdapter(FragmentManager fm, List<String> list) {
            super(fm);
            this.mlist = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mlist.get(position);
        }
    }
}
